package com.demo.homework;

import java.util.*;

/**
 * @ClassName Book
 * @Description TODO
 * @Author tianqinglong
 * @Date 18:58 2022/7/11
 * @Version 1.0
 **/
public class Book implements Comparable<Book>{
    private int bid;
    private String bname;
    private float price;

    public Book() {
    }

    public Book(int bid, String bname, float price) {
        this.bid = bid;
        this.bname = bname;
        this.price = price;
    }

    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bid=" + bid +
                ", bname='" + bname + '\'' +
                ", price=" + price +
                '}';
    }

    public static void main(String[] args) {
//        .TreeMap<Book, String> map = new TreeMap<>(new BookComparator());
        TreeMap<Book, String> map = new TreeMap<>();
        map.put(new Book(1,"西游记",100),"张三");
        map.put(new Book(2,"红楼梦",200),"李四");
        map.put(new Book(3,"水浒传",600),"王五");
        map.put(new Book(4,"三国演义",400),"赵六");
        map.put(new Book(5,"十宗罪",500),"田七");
//        map.keySet().stream().sorted((o1, o2) -> (int) (o1.getPrice()-o2.getPrice())).forEach(System.out::println);
        Iterator<Book> iterator = map.keySet().iterator();
        while (iterator.hasNext()){
            Book key = iterator.next();
            String s = map.get(key);
            System.out.println(key+"---"+s);
        }
        System.out.println("--------------------------");
        Set<Map.Entry<Book, String>> entries = map.entrySet();
        for (Map.Entry<Book, String> entry : entries) {
            System.out.println(entry.getKey()+"++++++++"+entry.getValue());
        }
    }

    @Override
    public int compareTo(Book o) {
        return (int) (this.getPrice()-o.getPrice());
    }
}/*
class BookComparator implements Comparator<Book>{
    @Override
    public int compare(Book o1, Book o2) {
        if(o1.getPrice()>o2.getPrice()) {
            return 1;
        } else if(o1.getPrice()<o2.getPrice()) {
            return -1;
        }
        return 0;

    }  }

*/
